package view;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import view.dialog.asbtract.AbstractDialog;
import view.dialog.i.DialogI;
import view.dialog.impl.JobInfoDialog;
import media.MediaConst;
import media.MediaUtil;
import model.Job;
import core.Const;

public class MainWindow extends JFrame {
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	JPanel panel;
	TrayIcon trayIcon;
	JButton btnStart;
	JButton btnEnd;
	JButton btnManageProject;
	JButton btnDetailsProject;
	JLabel lblProjectnameDisp;
	JLabel lblProjectStartTimeDisp;
	JLabel lblProjectDurationDisp;
	long time = 0;
	Timer timer;

	AbstractDialog<Job> detailDialog;

	public MainWindow() throws HeadlessException {
		super();
		this.panel = new JPanel(null);
		this.getContentPane().add(panel);

		initComponent();
		initIconTray();
		iniData();
		initListener();
		this.addFrameListener();

		this.setSize(MediaConst.MAINWINDOW_WIDTH, MediaConst.MAINWINDOW_HEIGHT);
		this.setIconImage(MediaUtil.getImage(MediaConst.CLOCK_ICON));
		this.setTitle(Const.APP_NAME + "  " + Const.VERSION);
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.setVisible(true);


		//timer.start();
	}

	private void initIconTray() {
		if (SystemTray.isSupported()) {

			SystemTray tray = SystemTray.getSystemTray();
			Image image = MediaUtil.getImage(MediaConst.CLOCK_ICON_SMALL);

			ActionListener exitListener = new ActionListener() {
				public void actionPerformed(ActionEvent e) {

					try {
						System.exit(0);
					} catch (Exception e1) {
						e1.printStackTrace();
					}
				}
			};

			PopupMenu popup = new PopupMenu();
			MenuItem defaultItem = new MenuItem("Exit");
			defaultItem.addActionListener(exitListener);
			popup.add(defaultItem);

			trayIcon = new TrayIcon(image,
					Const.APP_NAME + " " + Const.VERSION, popup);
			trayIcon.addMouseListener(new MouseAdapter() {

				@Override
				public void mouseClicked(MouseEvent e) {
					super.mouseClicked(e);

				}

			});

			trayIcon.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					MainWindow.this.setVisible(true);
				}
			});

			try {
				tray.add(trayIcon);
			} catch (AWTException e) {
			}
		} else {
		}
	}

	private void addFrameListener() {
		this.addWindowListener(new WindowAdapter() {

			@Override
			public void windowClosing(WindowEvent e) {
				// String answer = (String) JOptionPane.showInputDialog(
				// MainWindow.this,
				// Const.MESSAGE_CONFIRM_CLOSE,
				// Const.MESSAGE_CONFIRM_CLOSE_TITLE,
				// JOptionPane.QUESTION_MESSAGE,
				// null,
				// new String[] {
				// Const.COMMAND_EXIT,
				// Const.COMMAND_HIDE },
				// Const.COMMAND_HIDE);
				//
				// if (null == answer){
				// return;
				// }
				//
				// if (Const.COMMAND_EXIT.equals(answer)){
				// System.exit(0);
				// }

				// MainWindow.this.setVisible(false);
				// trayIcon.displayMessage(Const.APP_NAME_SHORT,
				// Const.MESSAGE_NOTIFY_ICON_PROGRAM, MessageType.INFO);
				System.exit(0);
			}
		});
	}

	private void initComponent() {
		btnStart = new JButton("Start");
		btnStart.setBounds(20, 370, 160, 50);
		panel.add(btnStart);


		btnEnd = new JButton("End");
		btnEnd.setBounds(210, 370, 160, 50);
		panel.add(btnEnd);


		JPanel panelDisp = new JPanel(null);
		panelDisp.setBounds(5, 5, MediaConst.MAINWINDOW_WIDTH - 25, 200);
		panelDisp.setBorder(BorderFactory.createEtchedBorder());
		panel.add(panelDisp);

		btnManageProject = new JButton("Manage projects...");
		btnManageProject.setBounds(210, 5, 160, 50);
		panelDisp.add(btnManageProject);

		btnDetailsProject = new JButton("Project details");
		btnDetailsProject.setBounds(210, 140, 160, 50);
		panelDisp.add(btnDetailsProject);

		JLabel lblProjectname = new JLabel("Project name:");
		JLabel lblProjectStartTime = new JLabel("Project start time:");

		lblProjectname.setFont(Const.FONT_LABEL);
		lblProjectname.setBounds(10, 60, 130, 30);
		panelDisp.add(lblProjectname);

		lblProjectnameDisp = new JLabel("project name example");
		lblProjectnameDisp.setFont(Const.FONT_VALUE_DISP);
		lblProjectnameDisp.setBounds(160, 60, 300, 30);
		lblProjectnameDisp.setToolTipText("tool tip example");
		panelDisp.add(lblProjectnameDisp);

		lblProjectStartTime.setFont(Const.FONT_LABEL);
		lblProjectStartTime.setBounds(10, 100, 140, 30);
		panelDisp.add(lblProjectStartTime);

		lblProjectStartTimeDisp = new JLabel("2012/12/24 15:00:00");
		lblProjectStartTimeDisp.setFont(Const.FONT_VALUE_DISP);
		lblProjectStartTimeDisp.setBounds(160, 100, 300, 30);
		lblProjectStartTimeDisp.setToolTipText("2012/12/24 15:00:00");
		panelDisp.add(lblProjectStartTimeDisp);

		JPanel panelDispTimeCountDown = new JPanel(null);
		panelDispTimeCountDown.setBounds(5, 210,
				MediaConst.MAINWINDOW_WIDTH - 25, 150);
		Border border = BorderFactory.createTitledBorder(
				BorderFactory.createEtchedBorder(EtchedBorder.RAISED),
				"Project duration");
		TitledBorder titleBorder = (TitledBorder) border;
		titleBorder.setTitleFont(Const.FONT_VALUE_DISP);
		panelDispTimeCountDown.setBorder(titleBorder);
		panelDispTimeCountDown.setBackground(Color.BLACK);
		panel.add(panelDispTimeCountDown);

		lblProjectDurationDisp = new JLabel();
		lblProjectDurationDisp.setFont(Const.FONT_DURATION_DISP);
		lblProjectDurationDisp.setBounds(15, 20, 400, 100);
		lblProjectDurationDisp.setForeground(Color.YELLOW);
		panelDispTimeCountDown.add(lblProjectDurationDisp);
	}

	private void iniData(){
		lblProjectnameDisp.setText("project name example");
		lblProjectnameDisp.setToolTipText("tool tip example");
		lblProjectStartTimeDisp.setText("2012/12/24 15:00:00");
		lblProjectStartTimeDisp.setToolTipText("2012/12/24 15:00:00");
		lblProjectDurationDisp.setText("000:00:00");

		btnStart.setEnabled(true);
		btnEnd.setEnabled(false);

		timer = new Timer(1000, new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				time += 1000;
				long second = (time / 1000) % 60;
				long minute = time / (1000 * 60) % 60;
				long hour = time / (1000 * 60 * 60);
				String secondStr = "" + second;
				if (secondStr.length() < 2) {
					secondStr = "0" + secondStr;
				}
				String minuteStr = "" + minute;
				if (minuteStr.length() < 2) {
					minuteStr = "0" + minuteStr;
				}
				String hourStr = "" + hour;

				while (hourStr.length() < 3) {
					hourStr = "0" + hourStr;
				}

				MainWindow.this.lblProjectDurationDisp.setText(hourStr + ":"
						+ minuteStr + ":" + secondStr);

			}
		});
	}

	private void initListener() {
		btnStart.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				lblProjectDurationDisp.setForeground(Color.GREEN);
				btnStart.setEnabled(false);
				btnEnd.setEnabled(true);
				timer.start();

			}
		});

		btnEnd.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				lblProjectDurationDisp.setForeground(Color.YELLOW);
				btnStart.setEnabled(true);
				btnEnd.setEnabled(false);
				timer.stop();

			}
		});

		btnDetailsProject.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (detailDialog == null) {
					detailDialog = new JobInfoDialog(MainWindow.this);
					detailDialog.showDialog();
				} else {
					detailDialog.showDialog();
				}
			}
		});
	}
}
